﻿Namespace IO
    Public Class Utils
        Public Shared Function IsSubDir(ByVal childPath As String, ByVal parentPath As String) As Boolean
            If Not childPath.EndsWith(System.IO.Path.PathSeparator) Then childPath = childPath & System.IO.Path.DirectorySeparatorChar
            If Not parentPath.EndsWith(System.IO.Path.PathSeparator) Then parentPath = parentPath & System.IO.Path.DirectorySeparatorChar
            Return childPath.ToLower.StartsWith(parentPath.ToLower)
        End Function

        Public Shared Function ReadTextFile(ByVal fileName As String) As String
            Using tr As New System.IO.StreamReader(fileName)
                Return tr.ReadToEnd()
            End Using
        End Function
        Public Shared Sub SaveTextFileUnicode(ByVal fileName As String, ByVal text As String)
            Using tr As New System.IO.StreamWriter(fileName, False, System.Text.Encoding.Unicode)
                tr.Write(text)
            End Using
        End Sub

        Public Class CleanedFileName
            Public Name As String
            Public Comment As String
        End Class

        Public Shared Function CleanUpFilename(ByVal fileName As String) As CleanedFileName
            Dim strComment As String = Nothing
            Dim iPos As Integer = fileName.IndexOfAny(New Char() {"["c, "{"c, "("c})
            Dim iCount As Integer = 0
            While iPos >= 0
                Dim iPos2 As Integer
                iCount += 1
                Select Case fileName.Chars(iPos)
                    Case "("c
                        'text inside parenthesis we remove and add to comment
                        iPos2 = Framework.Misc.FindBalancedClosingPosition(fileName, iPos + 1, "("c, ")"c)
                    Case "["c
                        'text inside brackets we remove and add to comment
                        iPos2 = Framework.Misc.FindBalancedClosingPosition(fileName, iPos + 1, "["c, "]"c)
                    Case "{"c
                        'text inside wavy brackets (whatever they're called..) we remove and add to comment
                        iPos2 = Framework.Misc.FindBalancedClosingPosition(fileName, iPos + 1, "{"c, "}"c)
                    Case Else
                        Throw New NotImplementedException
                End Select
                Dim strText As String
                If iPos2 = fileName.Length Then
                    strText = fileName.Substring(iPos + 1)
                    fileName = fileName.Substring(0, iPos).Trim
                Else
                    strText = fileName.Substring(iPos + 1, iPos2 - iPos - 1)
                    fileName = (fileName.Substring(0, iPos).Trim & " " & fileName.Substring(iPos2 + 1).Trim).Trim
                End If
                If iCount = 1 Then
                    strComment = strText
                ElseIf iCount = 2 Then
                    strComment = String.Format("({0}) ({1})", strComment, strText)
                Else
                    strComment = String.Format("{0} ({1})", strComment, strText)
                End If
                iPos = fileName.IndexOfAny(New Char() {"["c, "{"c, "("c})
            End While
            Dim clsRet As New CleanedFileName
            clsRet.Comment = strComment
            clsRet.Name = fileName
            Return clsRet
        End Function
    End Class

End Namespace

